home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Cheat II / cheatrick.c < prev    next >
Text File  |  1995-01-28  |  4KB  |  188 lines

  1.  // cheatrick.c
  2. // all the trap patch shit
  3.  
  4. #include "cheatrick.h"
  5. #include "cheatWindow.h"
  6. #include "main.h"
  7. #include "cheat.h"
  8. #include "ResRefs.h"
  9. #include "cheat.h"
  10. #include "cheatwich.h"
  11. #include "pascal.h"
  12. #include <string.h>
  13. #include "error.h"
  14. #include "Timer.h"
  15. #include "Processes.h"
  16.  
  17. #define commandkey 0x37
  18. #define optionkey 0x3A
  19. #define controlkey 0x3B
  20.  
  21.     // used for saving and restoring registers
  22. #define setupcode() asm {\
  23.                 movem.l    a0-a5/D0-D7, -(SP)\
  24.             }
  25. #define cleanupcode() asm {\
  26.                 movem.l    (SP)+, a0-a5/D0-D7\
  27.             }
  28.  
  29.  
  30.  
  31. static long oldtrap, oldtrapbutt;
  32. unsigned char km[16];
  33.  
  34.  
  35.  
  36.     // k =  any keyboard scan code, 0-127
  37. static int isPressed(unsigned short k )
  38. {
  39.     return ( ( km[k>>3] >> (k & 7) ) & 1);
  40. }
  41.  
  42. static void
  43. __GetA5(void)
  44. {
  45.     asm {
  46.         bsr.s    @1
  47.         dc.l    0            ;  store A4 here
  48. @1        move.l    (sp)+,a1
  49.     }
  50. }
  51.  
  52. #define RememberA5()    do { __GetA5(); asm { move.l a5,(a1) } } while (0)
  53. #define RecallA5()        do { __GetA5(); asm { move.l (a1),a5 } } while (0)
  54.  
  55.  
  56. pascal void newGetKeys(KeyMap theKeys)
  57. {
  58.     long oura5;
  59.     
  60.     setupcode();
  61.     RecallA5();
  62.     
  63.     CallPascal(theKeys, oldtrap);
  64. /*    asm{
  65.         move.l theKeys, km
  66.         }  */
  67.         // my sneaky asm code doesn't seem to work, so . . .
  68.     *((long *)&km[0]) = theKeys[0];
  69.     *((long *)&km[4]) = theKeys[1];
  70.     *((long *)&km[8]) = theKeys[2];
  71.     *((long *)&km[12]) = theKeys[3];
  72.     if (isPressed(controlkey) && isPressed(commandkey) && isPressed(optionkey)) {
  73.             // they've hit the key combo to enter our program
  74.         if (!cheatwich(oldtrap))
  75.             SysBeep(1);
  76.     }
  77.     
  78.     cleanupcode();
  79. }
  80.  
  81. pascal short newButton()
  82. {
  83.     long oura5;
  84.     KeyMap theKeys;
  85.     short res;
  86.     
  87.     setupcode();
  88.     RecallA5();
  89.     
  90.     CallPascal((long *) km, oldtrap);
  91.     if (isPressed(controlkey) && isPressed(commandkey) && isPressed(optionkey)) {
  92.             // they've hit the key combo to enter our program
  93.         if (!cheatwich(oldtrap))
  94.             SysBeep(1);
  95.     }
  96.     
  97.     res = CallPascalW(oldtrapbutt);
  98.     cleanupcode();
  99.     return res;
  100. }
  101.  
  102. typedef struct {
  103.     struct TMTask atm;
  104.     long tmWakeUp;
  105.     long tmReserved;
  106.     long tmRefCon;
  107. } mytminfo;
  108.  
  109. mytminfo tmi;
  110. long del = 4000;
  111.     
  112.     // our delightful time manager task
  113. pascal void mytask(void)
  114. {
  115.     mytminfo *rec;
  116.     long oldA5;
  117.     long newdelay;
  118.     
  119.     ProcessSerialNumber psn;
  120.     OSErr err;
  121.     int i, newone;
  122.     Boolean res;
  123.     
  124.     
  125.         // get our data
  126.     asm {
  127.         move.l A1, rec\
  128.     }
  129.     setupcode();
  130.     oldA5 = SetA5(rec->tmRefCon);
  131.     
  132.     err = GetFrontProcess(&psn);
  133.     newone = false;
  134.     if (!err) {
  135.         newone = true;        // assume unique
  136.             // check if it's our app specially
  137.         err = SameProcess(&psn, &usPSN, &res);
  138.         if (!err && res)
  139.             newone = false;        // it's Cheat itself
  140.         else
  141.             for (i=0;i<numProcs;i++) {
  142.                 err = SameProcess(&psn, &InfoRec[i].processNumber, &res);
  143.                 if (!err && res)
  144.                     newone = false;        // already found
  145.             }
  146.         if (newone) {
  147.                 // patch the traps
  148.             SetToolTrapAddress((long) newGetKeys, 0x176);
  149.             if (prefs.shouldpatchbutt)
  150.                 SetToolTrapAddress((long) newButton, 0x174);
  151.         }
  152.     }
  153.     if (!newone) {        // install it again and wait
  154.         newdelay = 4000;
  155.         PrimeTime((QElemPtr) rec, newdelay);
  156.     }    // else we have patched one thing, that's enough for today
  157.     
  158.     cleanupcode();
  159. }
  160.  
  161.     // put the task in the queue
  162. void installTMTask(void)
  163. {
  164.     RememberA5();        // used in trap patch
  165.  
  166.     shouldclearup = false;
  167.     
  168.     tmi.atm.tmAddr = (void *) mytask;
  169.     tmi.tmWakeUp = 0;
  170.     tmi.tmReserved = 0;
  171.     tmi.tmRefCon = SetCurrentA5();
  172.     InsTime((QElemPtr) &tmi);
  173.     PrimeTime((QElemPtr) &tmi, del);
  174. }
  175.  
  176. void removeTMTask(void)
  177. {
  178.     RmvTime((QElemPtr) &tmi);
  179. }
  180.  
  181. void remembertraps(void)
  182. {
  183.         // used for getting key stuff, needs to be changed eventually, really
  184.     oldtrap = GetToolTrapAddress(0x176);
  185.     verify(oldtrap);
  186.     oldtrapbutt = GetToolTrapAddress(0x174);
  187.     verify(oldtrapbutt);
  188. }